import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsNumber,
  IsBoolean,
  IsEnum,
  IsArray,
  IsDateString,
  MaxLength,
  MinLength,
  Min,
  Max
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';

// 创建博客 DTO
export class CreateBlogDto {
  @ApiProperty({
    description: '博客标题',
    example: '学习 NestJS 框架',
    maxLength: 255
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(255)
  title: string;

  @ApiProperty({
    description: '博客内容',
    example: '这是一篇关于 NestJS 框架的详细介绍...'
  })
  @IsString()
  @IsNotEmpty()
  content: string;

  @ApiPropertyOptional({
    description: '博客摘要',
    example: 'NestJS 是一个用于构建高效、可扩展的 Node.js 服务器端应用程序的框架...'
  })
  @IsOptional()
  @IsString()
  summary?: string;

  @ApiPropertyOptional({
    description: '封面图',
    example: 'https://example.com/cover.jpg',
    maxLength: 500
  })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  cover?: string;

  @ApiProperty({
    description: '作者',
    example: 'John Doe',
    maxLength: 100
  })
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  author: string;

  @ApiProperty({
    description: '作者ID',
    example: 1
  })
  @IsNumber()
  authorId: number;

  @ApiProperty({
    description: '分类',
    example: '技术',
    enum: ['技术', '生活', '随笔', '教程']
  })
  @IsEnum(['技术', '生活', '随笔', '教程'])
  category: '技术' | '生活' | '随笔' | '教程';

  @ApiPropertyOptional({
    description: '状态',
    example: 1,
    enum: [0, 1],
    default: 1
  })
  @IsOptional()
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '是否置顶',
    example: false,
    default: false
  })
  @IsOptional()
  @IsBoolean()
  isTop?: boolean;

  @ApiPropertyOptional({
    description: '阅读量',
    example: 100,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  viewCount?: number;

  @ApiPropertyOptional({
    description: '点赞数',
    example: 10,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  likeCount?: number;

  @ApiPropertyOptional({
    description: '评论数',
    example: 5,
    default: 0
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  commentCount?: number;

  @ApiPropertyOptional({
    description: '发布时间',
    example: '2024-01-01T00:00:00.000Z'
  })
  @IsOptional()
  @IsDateString()
  publishedAt?: string;

  @ApiPropertyOptional({
    description: 'SEO 标题',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  metaTitle?: string;

  @ApiPropertyOptional({
    description: 'SEO 描述'
  })
  @IsOptional()
  @IsString()
  metaDescription?: string;

  @ApiPropertyOptional({
    description: 'SEO 关键词',
    maxLength: 500
  })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  metaKeywords?: string;

  @ApiPropertyOptional({
    description: '阅读时间（分钟）',
    example: 5
  })
  @IsOptional()
  @IsNumber()
  @Min(0)
  readingTime?: number;

  @ApiPropertyOptional({
    description: 'URL 别名',
    example: 'learning-nestjs-framework',
    maxLength: 255
  })
  @IsOptional()
  @IsString()
  @MaxLength(255)
  slug?: string;

  @ApiPropertyOptional({
    description: '是否允许评论',
    example: true,
    default: true
  })
  @IsOptional()
  @IsBoolean()
  allowComment?: boolean;

  @ApiPropertyOptional({
    description: '标签ID数组',
    type: [Number],
    example: [1, 2, 3]
  })
  @IsOptional()
  @IsArray()
  @IsNumber({}, { each: true })
  tagIds?: number[];
}

// 更新博客 DTO
export class UpdateBlogDto extends PartialType(CreateBlogDto) {}

// 查询博客 DTO
export class QueryBlogsDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 10;

  @ApiPropertyOptional({
    description: '搜索关键词（标题、内容、摘要）',
    example: 'NestJS'
  })
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional({
    description: '分类',
    enum: ['技术', '生活', '随笔', '教程']
  })
  @IsOptional()
  @IsEnum(['技术', '生活', '随笔', '教程'])
  category?: '技术' | '生活' | '随笔' | '教程';

  @ApiPropertyOptional({
    description: '状态',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '作者ID',
    example: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  authorId?: number;

  @ApiPropertyOptional({
    description: '标签ID',
    example: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  tagId?: number;

  @ApiPropertyOptional({
    description: '是否置顶',
    example: true
  })
  @IsOptional()
  @Type(() => Boolean)
  @IsBoolean()
  isTop?: boolean;

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'createdAt',
    enum: ['createdAt', 'updatedAt', 'publishedAt', 'title', 'viewCount', 'likeCount']
  })
  @IsOptional()
  @IsString()
  sortBy?: string = 'createdAt';

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'DESC',
    enum: ['ASC', 'DESC'],
    default: 'DESC'
  })
  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC' = 'DESC';
}

// 批量操作博客 DTO
export class BatchBlogActionDto {
  @ApiProperty({
    description: '博客ID数组',
    type: [Number],
    example: [1, 2, 3]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  blogIds: number[];

  @ApiProperty({
    description: '操作类型',
    enum: ['publish', 'unpublish', 'top', 'untop', 'delete', 'changeCategory']
  })
  @IsString()
  @IsEnum(['publish', 'unpublish', 'top', 'untop', 'delete', 'changeCategory'])
  action: 'publish' | 'unpublish' | 'top' | 'untop' | 'delete' | 'changeCategory';

  @ApiPropertyOptional({
    description: '目标分类（用于更改分类操作）',
    enum: ['技术', '生活', '随笔', '教程']
  })
  @IsOptional()
  @IsEnum(['技术', '生活', '随笔', '教程'])
  targetCategory?: '技术' | '生活' | '随笔' | '教程';
}
